package weibo.config;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
import weibo.common.interceptor.GheckLoginInterceptor;

import java.io.Serializable;

@Configuration
@MapperScan("weibo.mapper")
@EnableSwagger2WebMvc
public class Config implements WebMvcConfigurer {


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new GheckLoginInterceptor())
                .addPathPatterns("v1/users/currentUser","v1/weibo/insert","/v1/comment/insert");
    }


    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                //用来配置文档信息
                .apiInfo(apiInfo())
                //用于定义为我们项目的哪个包下的类生成文档(通常仅指定controller包)
                .select().apis(RequestHandlerSelectors.basePackage("weibo.controller"))
                //指定为哪些url生成文档，这里为所有
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                //当前项目的名字(生成的文档网页的标题)
                .title("微博项目")
                //当前项目的描述(文档描述)
                .description("微博项目在线API文档")
                //文档版本
                .version("1.0")
                //联系人
                .contact(new Contact("yigei","www.weibo.cn","yigei@qq.com"))
                //生成文档信息
                .build();
    }

    // 配置RedisTemplate 模板
    @Bean
    public RedisTemplate<String, Serializable> initReadisTemplate(
            RedisConnectionFactory redisConnectionFactory
    ){
        // 初始化Redis 模板类
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        // 为模板类设置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // key 序列化为字符串显示
        redisTemplate.setKeySerializer(RedisSerializer.string());
        // 设置Valus的序列化方式
        redisTemplate.setValueSerializer(RedisSerializer.json());
        // 设置hash的key序列化方式
        redisTemplate.setHashKeySerializer(RedisSerializer.json());
        redisTemplate.setHashValueSerializer(RedisSerializer.json());

        return redisTemplate;

    }

}
